package cn.aran.controller;

import cn.aran.domain.Product;
import cn.aran.domain.Store;
import cn.aran.service.IStoreService;
import cn.aran.utils.AjaxResult;
import cn.aran.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/store")
public class StoreController {
    @Autowired
    private IStoreService storeService;


    @PostMapping("/add")
    public AjaxResult addStore(@RequestBody ProductVo productvo){
        try {
            Store store = new Store();
            BeanUtils.copyProperties(productvo, store);
            Product product = new Product();
            BeanUtils.copyProperties(productvo, product);

            store.setProductId(productvo.getProductId());
            Store stoTemp = storeService.loadByProductId(productvo.getProductId());
            if (stoTemp != null){
                store.setNumber(stoTemp.getNumber() + productvo.getNumber());
                storeService.remove(stoTemp.getId());
            }else {
                store.setNumber(productvo.getNumber());
            }
            storeService.add(store);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("添加失败!");
        }
    }
}
